import React from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import authApi from '../../api/auth';

interface PrivateRouteProps {
    children: React.ReactNode;
}

const PrivateRoute: React.FC<PrivateRouteProps> = ({ children }) => {
    const location = useLocation();

    if (!authApi.isAuthenticated()) {
        // 重定向到登录页，并记录原始访问路径，以便登录后可以返回
        return <Navigate to="/login" state={{ from: location }} replace />;
    }

    return <>{children}</>;
};

export default PrivateRoute; 